package org.example.day11;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.11.1 方法hasQueuedThread(Thread thread)
 *      ①hasQueuedThread(Thread thread): 查询某线程是否正在等待此锁.
 *      ②hasQueuedThreads(): 查询是否有线程正在等待获取此锁
 */
public class MyThread10 {
    public ReentrantLock lock = new ReentrantLock();
    public Condition newCondition = lock.newCondition();
    public void waitMethod(){
        try {
            lock.lock();
            Thread.sleep(Integer.MAX_VALUE);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final MyThread10 m10 = new MyThread10();
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                m10.waitMethod();
            }
        };
        Thread ta = new Thread(runnable);
        ta.start();
        Thread.sleep(500); //ta先拿到锁
        Thread tb = new Thread(runnable);
        tb.start();
        Thread.sleep(500);
        System.out.println(m10.lock.hasQueuedThread(ta));
        System.out.println(m10.lock.hasQueuedThread(tb));
        System.out.println(m10.lock.hasQueuedThreads());
    }
}
